import ActionMethodsBase from "./ActionMethodsBase";
import actionRegistrar from "./ActionRegistrar";
import ActionParam from "./ActionParam";

export type ActionFunType = ((instance: Action) => void) | undefined
export type ActionHandleType = (actionParam: ActionParam) => any

class Action extends ActionMethodsBase {
    name = ''
    path: string = '/'
    fun: ActionFunType

    /**
     * @param {string} name 名称
     * @param {ActionFunType} fun 路由执行方法
     */
    constructor(name: string, fun: ActionFunType) {
        super();
        if (!name) {
            throw new Error('name is required')
        }
        this.name = name;
        this.fun = fun;
        if (fun && typeof fun === 'function') {
            fun(this)
        }
    }

    /**
     * action 路径
     * @param {string} path 路径
     */
    mapping(path: string) {
        this.path = path;
    }

    /**
     * 自动注入到 node action
     */
    autoInject() {
        actionRegistrar.adds([this])
    }
}

export default Action
